package com.zz.util;

import org.json.JSONException;
import org.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

public class HttpUtil {
    public static void main(String[] args) throws IOException {
        HttpUtil httpUtil=new HttpUtil();
        httpUtil.test1();
    }

    /**
     * 1. 静态方法可以直接调用静态方法
     * 2. 静态方法不能直接调用非静态方法，必须先创建对象，通过对象执行非静态方法
     * 3. 非静态方法可以直接调用同一类下面的 静态和非静态方法
     * 4. 非静态方法调用另外一类的静态方法的时候，需要用另外类的类名.静态方法名字
     * 5. 非静态方法调用另外一类的非静态方法的时候，必须先创建对象，通过对象执行非静态方法
     */
    public void test2() throws IOException {
        test1();
    }
    public void test1() throws IOException {
        String url="https://www.baidu.com/";
        URL urlGet = new URL(url);
        HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
        http.setRequestMethod("GET"); //必须是get方式请求
        http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        http.setDoOutput(true);
        http.setDoInput(true);
        System.setProperty("javax.net.ssl.keyStore", "");
        System.setProperty("javax.net.ssl.keyStorePassword", "");
        System.setProperty("javax.net.ssl.trustStore", "");
        System.setProperty("javax.net.ssl.trustStorePassword","");
        System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
        System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
        http.connect();
        // 获取请求的返回值
        InputStream is =http.getInputStream();
        int size =is.available();
        byte[] jsonBytes =new byte[size];
        is.read(jsonBytes);
        String message=new String(jsonBytes,"UTF-8");

        System.out.println("msg====="+message);

    }


    public static HashMap<String,String> getOpenIdAndToken(String code, String appid, String sid){
        String openId="";
        String access_token="";
        HashMap<String,String> result=new HashMap();
        String errcode="";
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+sid+"&code="+code+"&grant_type=authorization_code";
        URL urlGet;
        String msg;
        try {
            urlGet = new URL(url);
            HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();

            http.setRequestMethod("GET"); //必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("javax.net.ssl.keyStore", "");
            System.setProperty("javax.net.ssl.keyStorePassword", "");
            System.setProperty("javax.net.ssl.trustStore", "");
            System.setProperty("javax.net.ssl.trustStorePassword","");
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
            http.connect();
            InputStream is =http.getInputStream();
            int size =is.available();
            byte[] jsonBytes =new byte[size];
            is.read(jsonBytes);
            String message=new String(jsonBytes,"UTF-8");
            JSONObject demoJson;
            try {
                demoJson = new JSONObject(message);
                msg=demoJson.toString();
                if(msg.indexOf("openid")!=-1){
                    openId = demoJson.getString("openid");
                    access_token = demoJson.getString("access_token");
                    System.out.println("access_token="+access_token);
                    result.put("openId",openId);
                    result.put("access_token",access_token);
                }else if(msg.indexOf("errcode")!=-1){
                    errcode=demoJson.getInt("errcode")+"===="+demoJson.getString("errmsg");
                }
                System.out.println("openId====="+openId+"errcode====="+errcode);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
